package de.lianowar.whereismymoney.fragment;





import de.lianowar.whereismymoney.R;
import de.lianowar.whereismymoney.adapter.AccountListAdapter;
import de.lianowar.whereismymoney.sql.DatabaseHandler;
import de.lianowar.whereismymoney.sql.data.Account;
import de.lianowar.whereismymoney.sql.data.MoneyAmount;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;



@SuppressLint("ValidFragment")
public class Fra_Insert extends Fragment
{
	public Button save;
	public TextView comment;
	public TextView ammount;
	public TextView date;
	private CheckBox incomeMoney;
	private CheckBox outgoingMoney;
	private Spinner s;
	private DatabaseHandler dbHandler = null;
	private AccountListAdapter adapter;
	private Account tempAccount;
	
	public Fra_Insert(){}
	
	public Fra_Insert(DatabaseHandler pDbHandler)
	{
		
		dbHandler = pDbHandler;

		Log.e("GUI", "Konstruktor");
	}
			
	
	@Override	
	 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		    View view = inflater.inflate(R.layout.la_ta_insert,container, false);		    

	        prepareComponents(view);
	        prepareEvents(); 		
	        adapter = new AccountListAdapter(this.getActivity(),dbHandler.getAllAccounts());
	        s.setAdapter(adapter);
	        
	        //test
	       
	    
	        
		    return view;
	}
	
	private void prepareEvents()
	{
		save.setOnClickListener(new View.OnClickListener()
		{
			@Override
				public void onClick(View view)
				{		
				try {
						MoneyAmount m = new MoneyAmount();
						m.setComment(comment.getText().toString());
						float tmp = Float.parseFloat(ammount.getText().toString());
						if(outgoingMoney.isChecked())
						{
							tmp = tmp *  -1;
						}
						m.setAmount(tmp);
						
						m.setDate(date.getText().toString());
						m.setAccount_fk(tempAccount.getId());
						dbHandler.addMoneyAmount(m);
						
						ammount.setText("");
						date.setText("");
						comment.setText("");
						
						Intent test = new Intent();
						test.setAction("YourAwesomeAction2");
						PendingIntent mPendingIntent = PendingIntent.getBroadcast(view.getContext(), 0, test, 0);
					
						mPendingIntent.send();
					} catch (CanceledException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		 s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
			        Log.e("SELECTED INSERT", ((Account)adapter.getItem(i)).getName() );
			        tempAccount = (Account)adapter.getItem(i);
			    } 

			    public void onNothingSelected(AdapterView<?> adapterView) {
			        return;
			    } 
			}); 
		 
		 incomeMoney.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View v) {
	                // TODO Auto-generated method stub
	                if(incomeMoney.isChecked()){
	                    Log.e("CHECKBOX", "Checked");
	                    outgoingMoney.setChecked(false);
	                }else{
	                	 Log.e("CHECKBOX", "Un-Checked");
	                }
	            }
	        });
		 outgoingMoney.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View v) {
	                // TODO Auto-generated method stub
	                if(incomeMoney.isChecked()){
	                    incomeMoney.setChecked(false);
	                }else{
	                	 Log.e("CHECKBOX", "Un-Checked");
	                }
	            }
	        });
	}
	
	private void prepareComponents(View pView)
	{
		    save = (Button)pView.findViewById(R.id.buSave);		    
		    comment= (TextView)pView.findViewById(R.id.etComment);
		    ammount = (TextView)pView.findViewById(R.id.etAmount);
		    date = (TextView)pView.findViewById(R.id.etDate);
		    s = (Spinner)pView.findViewById(R.id.spAccount);
		    incomeMoney = (CheckBox)pView.findViewById(R.id.checkBox1);
		    outgoingMoney = (CheckBox)pView.findViewById(R.id.checkBox2);
	}
	
}
